﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 评价
    /// </summary>
    public class CommentController : ApiController
    {
        private ApiContext _db = new ApiContext(); 
        /// <summary>
        /// 新增评论
        /// </summary>
        /// <param name="model">评论实体</param>
        /// <returns></returns>
        public JsonData Post([FromBody]U_Comment model)
        {
            var jsonData = new JsonData { Success = true }; 
            try
            {
                if (ModelState.IsValid)
                {
                    model.CommentTime = DateTime.Now;
                    
                    var item = _db.Comment.Add(model);

                    _db.SaveChanges();

                    var orderbll = new RBS.Core.BLL.D_Order();
                    var ordermodel = orderbll.GetModel(Convert.ToInt32(model.Column_10)); 
                    ordermodel.OrderState = 6; 
                    orderbll.Update(ordermodel);
                    jsonData.Success = true;
                    jsonData.Data = null;
                
                }
            }
            catch
            {
                jsonData.Success = false;
                jsonData.Error = "评论失败";
            }
            RBS.Core.Logs.LogManager.WriteLog("座位信息", ConvertJson.EntityToJson(jsonData)); ;
            return jsonData;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">用户或者酒店ID</param>
        /// <param name="type">hotel，user</param>
        /// <param name="page">页码</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        public JsonDataList GetComment(int id,string type, int page = 1, int limit = 5)
        {
       
            if (type == "hotel")
            {
                return GetHotelComment(id, page, limit);
            }
            if (type == "user")
            {
                return GetUserComment(id, page, limit);
            }
            return null;
        } 
        private JsonDataList GetHotelComment(int id, int page = 1, int limit = 5)
        {
            var jsonDataList = new JsonDataList { Success = true };
            var items = _db.VComment.Where(c => c.HotelId == id).OrderByDescending(c => c.CommentTime); 
          
           int total = items.Count();
           double pageCount = Math.Ceiling((double)total / (double)limit);
           int totalPage = (int)pageCount;
           if (page > totalPage)
           {
               page = totalPage;
               jsonDataList.Data = null;
               jsonDataList.Count = 0;
               jsonDataList.Page = page;
               jsonDataList.PageSize = limit;
               jsonDataList.TotalPage = totalPage;
               jsonDataList.Success = false;
               jsonDataList.Error = "最大页数为：" + totalPage + "页";
               return jsonDataList;
           }

           jsonDataList.Data = items.Skip((page - 1) * limit).Take(limit).ToList();  
           jsonDataList.Count = total;
           jsonDataList.Page = page;
           jsonDataList.PageSize = limit;
           jsonDataList.TotalPage = totalPage; 
           return jsonDataList;
        }
        private JsonDataList GetUserComment(int id, int page = 1, int limit = 5)
        {
            var jsonDataList = new JsonDataList { Success = true };
            var items = _db.VComment.Where(c => c.UserId == id).OrderByDescending(c => c.CommentTime);

            int total = items.Count();
            double pageCount = Math.Ceiling((double)total / (double)limit);
            int totalPage = (int)pageCount;
            if (page > totalPage)
            {
                page = totalPage;
                jsonDataList.Data = null;
                jsonDataList.Count = 0;
                jsonDataList.Page = page;
                jsonDataList.PageSize = limit;
                jsonDataList.TotalPage = totalPage;
                jsonDataList.Success = false;
                jsonDataList.Error = "最大页数为：" + totalPage + "页";
                return jsonDataList;
            }
            jsonDataList.Data = items.Skip((page - 1) * limit).Take(limit).ToList();
            jsonDataList.Count = total;
            jsonDataList.Page = page;
            jsonDataList.PageSize = limit;
            jsonDataList.TotalPage = totalPage;
            return jsonDataList;
        }

    }
}
